package com.hope.bubble.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hope.bubble.mapper.BubbleTagMapper;
import com.hope.bubble.model.entity.BubbleTag;
import com.hope.bubble.service.IBubblePostService;
import com.hope.bubble.service.IBubbleTagService;
import com.hope.bubble.service.IBubbleTopicTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件名：BubbleTagServiceImpl
 * 创建者：hope
 * 邮箱：1602774287@qq.com
 * 微信：hope4cc
 * 创建时间：2022/10/22-15:51
 * 描述：
 */
@Service
public class BubbleTagServiceImpl extends ServiceImpl<BubbleTagMapper, BubbleTag> implements IBubbleTagService {
    @Autowired
    private IBubbleTopicTagService BmsTopicTagService;

    @Autowired
    private IBubblePostService BmsPostService;


    @Override
    public List<BubbleTag> insertTags(List<String> tagNames) {
        //创建一个标签列别对象
        List<BubbleTag> tagList = new ArrayList<>();
        //将前端传过来的 标签名字 对比数据库的标签表
        for (String tagName : tagNames) {
            BubbleTag tag = this.baseMapper.selectOne(new LambdaQueryWrapper<BubbleTag>().eq(BubbleTag::getName, tagName));
            if (tag == null) { //如果为空,那就说明之前没有创建过
                tag = BubbleTag.builder().name(tagName).build(); //创建标签对象
                this.baseMapper.insert(tag);//插入数据库
            } else {
                tag.setTopicCount(tag.getTopicCount() + 1);//如果标签已存在,那就将标签的话题数加一
                this.baseMapper.updateById(tag);//更新以上操作
            }
            tagList.add(tag);
        }
        return tagList;
    }




}
